package com.yuesystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuesystem.common.R;
import com.yuesystem.dto.BlogFront;
import com.yuesystem.dto.requestbody.LikeOrNotRequestBody;
import com.yuesystem.dto.responsebody.BlogResponseBody;
import com.yuesystem.dto.responsebody.CommentResponseBody;
import com.yuesystem.dto.responsebody.ReplyResponseBody;
import com.yuesystem.entity.Blog;
import com.yuesystem.entity.Comment;
import com.yuesystem.entity.Reply;
import com.yuesystem.service.BlogService;
import com.yuesystem.service.BrowseHistoryService;
import com.yuesystem.service.CollectBlogService;
import com.yuesystem.service.CollectItemService;
import com.yuesystem.service.CommentService;
import com.yuesystem.service.LikeService;
import com.yuesystem.service.ReplyService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 帖子类.
 */
@RestController
@CrossOrigin
@RequestMapping("/blogs")
@Slf4j
public class BlogController {
  @Autowired
  private ReplyService replyService;

  @Autowired
  private BlogService blogService;

  @Autowired
  private LikeService likeService;

  @Autowired
  private CommentService commentService;

  @Autowired
  private CollectBlogService collectBlogService;

  @Autowired
  private BrowseHistoryService browseHistoryService;

  @Autowired
  private CollectItemService collectItemService;


  /**
   * 发布帖子.
   *
   * @param blog 帖子实体类
   * @return 是否发布成功
   */
  @PostMapping("/saveBlog")
  public R<String> saveBlog(@RequestBody Blog blog) {
    log.info(blog.toString());
    blogService.saveBlog(blog);
    return R.success("帖子发布成功！");
  }

  /**
   * 评论帖子.
   *
   * @param comment 评论实体类
   * @return 是否评论成功
   */
  @PostMapping("/saveComment")
  public R<String> saveComment(@RequestBody Comment comment) {
    // 同时更新帖子的最新回复时间
    Long blogId = comment.getBlogId();
    Blog blog = blogService.getById(blogId);
    blog.setNewestCommentTime(LocalDateTime.now());
    blogService.updateById(blog);
    commentService.saveComment(comment);
    return R.success("评论成功");
  }

  /**
   * 回复评论.
   *
   * @param reply 回复实体类
   * @return 是否操作成功
   */
  @PostMapping("/replyComment")
  public R<String> replyComment(@RequestBody Reply reply) {
    // 同时更新帖子的最新回复时间
    Long blogId = reply.getBlogId();
    Blog blog = blogService.getById(blogId);
    blog.setNewestCommentTime(LocalDateTime.now());
    blogService.updateById(blog);
    // 帖子id,被回复的评论的id,回复内容
    replyService.replyComment(reply);
    return R.success("回复成功");
  }

  /**
   * 判断当前用户点赞帖子过该帖子.
   *
   * @param blogId 帖子id
   * @return 是否操作成功
   */
  @GetMapping("/like/judge/{blogId}")
  public R<Boolean> judge(@PathVariable Long blogId) {
    Boolean flag = likeService.judge(blogId);
    return R.success(flag);
  }

  /**
   * 用户点赞或取消点赞帖子.
   *
   * @return 是否操作成功
   */
  @PutMapping("/like/addOrCancel")
  public R<String> likeOrCancel(@RequestBody LikeOrNotRequestBody likeOrNotRequestBody) {
    Boolean flag = likeOrNotRequestBody.getFlag();
    Long blogId = likeOrNotRequestBody.getBlogId();
    if (flag) {
      // 调用取消点赞函数
      likeService.cancel(blogId);
      return R.success("取消成功");
    } else {
      // 调用添加点赞函数
      likeService.add(blogId);
      return R.success("点赞成功");
    }
  }

  /**
   * 根据帖子id,返回该帖子的点赞数量.
   *
   * @param blogId 帖子id
   * @return 帖子点赞数量
   */
  @GetMapping("/like/counts/{blogId}")
  public R<Long> likeCounts(@PathVariable Long blogId) {
    Long counts = likeService.getLikeCount(blogId);
    return R.success(counts);
  }

  /**
   * 根据传入的帖子id,统计该帖子的总评论数量.
   *
   * @param blogId 帖子id
   * @return 总评论数量.
   */
  @GetMapping("/comment/counts/{blogId}")
  public R<Long> commentCounts(@PathVariable Long blogId) {
    Long counts = commentService.getCommentCount(blogId);
    return R.success(counts);
  }

  /**
   * 分页查询帖子（首页）.
   *
   * @param page     起始页
   * @param pageSize 页数
   * @param type0    旅游攻略
   * @param type1    新闻资讯
   * @param type2    书院趣事
   * @param type3    官职相关
   * @return 帖子响应类的分页形式
   */
  @GetMapping("/list")
  public R<Page<BlogResponseBody>> getBlog(int page, int pageSize, String type0,
                                           String type1, String type2, String type3) {
    BlogFront blogFront = new BlogFront(page, pageSize, type0, type1, type2, type3);
    Page<BlogResponseBody> pageInfo = blogService.getPage(blogFront);
    return R.success(pageInfo);
  }

  /**
   * 帖子对应的所有一级评论.
   *
   * @param blogId 帖子评论
   * @return 评论响应类的列表
   */
  @GetMapping("/commentFirst/{blogId}")
  public R<List<CommentResponseBody>> getCommentFist(@PathVariable Long blogId) {
    List<CommentResponseBody> commentResponseBodyList = commentService.getCommentFist(blogId);
    return R.success(commentResponseBodyList);
  }

  /**
   * 一级评论下的二级评论.
   *
   * @param blogId          帖子评论
   * @param parentCommentId 一级评论类id
   * @return 回复响应类的列表
   */
  @GetMapping("/commentTwo/{blogId}/{parentCommentId}")
  public R<List<ReplyResponseBody>> getCommentFist(@PathVariable Long blogId,
                                                   @PathVariable Long parentCommentId) {
    List<ReplyResponseBody> replyResponseBodyList =
        replyService.getCommentTwo(blogId, parentCommentId);
    return R.success(replyResponseBodyList);
  }

  /**
   * 展示一条帖子所有内容.
   *
   * @param blogId 帖子id
   * @return 帖子展示类
   */
  @GetMapping("/blogOneList/{blogId}")
  public R<BlogResponseBody> blogOne(@PathVariable Long blogId) {
    BlogResponseBody blogResponseBody = blogService.blogOne(blogId);
    return R.success(blogResponseBody);
  }

  /**
   * 查看当前用户已经发布的帖子.
   *
   * @param page      起始类
   * @param pageSize  页数
   * @param startTime 起始时间
   * @param endTime   结束时间
   * @return 帖子展示类的分页形式
   */
  @GetMapping("/myBlogs")
  public R<Page<BlogResponseBody>> getPageMy(int page, int pageSize,
                                             String startTime, String endTime) {
    Page<BlogResponseBody> pageInfo = blogService.getPageMyBlog(page, pageSize, startTime, endTime);
    return R.success(pageInfo);
  }

  /**
   * 根据帖子id,返回所有的以及一级评论和二级评论.
   *
   * @param blogId 帖子id
   * @return 所有评论（一级+二级）
   */
  @GetMapping("/comment/{blogId}")
  public R<List<CommentResponseBody>> getComment(@PathVariable Long blogId) {
    List<CommentResponseBody> commentList = blogService.getComment(blogId);
    return R.success(commentList);
  }

  /**
   * 删除我的发布(删除帖子前，先把相关的所有点赞、收藏、评论，回复给删除掉).
   *
   * @param blogId 帖子id
   * @return 删除时的提示信息
   */
  @DeleteMapping("/deleteMyBlogById/{blogId}")
  public R<String> deleteMyBlogById(@PathVariable Long blogId) {
    List<Long> listBlogs = new ArrayList<>();
    listBlogs.add(blogId);
    // 从回复表中删除有关这个帖子的回复
    replyService.deleteByBlog(listBlogs);
    // 从评论表中删除有关这个帖子的评论
    commentService.deleteByBlog(listBlogs);
    // 从收藏表中删除有关这个帖子的收藏记录
    collectItemService.deleteByBlog(listBlogs);
    //collectBlogService.deleteByBlog(listBlogs);
    // 从like表中删除有关这个帖子的点赞记录
    likeService.deleteByBlog(listBlogs);
    // 从浏览记录表删除有关这个帖子的浏览记录
    browseHistoryService.deleteByBlog(listBlogs);
    // 删除帖子
    LambdaQueryWrapper<Blog> queryWrapper1 = new LambdaQueryWrapper<>();
    queryWrapper1.eq(Blog::getId, blogId);
    blogService.remove(queryWrapper1);
    return R.success("删除成功！");
  }

}


